package com.demo.apparel.mapper.web;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.apparel.entity.AdminInfo;
import com.demo.apparel.vo.web.AdminVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AdminInfoMapper extends BaseMapper<AdminInfo> {

    @Select("SELECT id, admin_id, nickname, avatar, email, gender, created_time, updated_time " +
            "FROM admin_info WHERE admin_id = #{adminId}")
    AdminInfo selectByAdminId(@Param("adminId") String adminId);

    @Insert("INSERT INTO admin_info (id, admin_id, nickname, avatar, email, gender, created_time, updated_time) " +
            "VALUES (#{id}, #{adminId}, #{nickname}, #{avatar}, #{email}, #{gender}, NOW(), NOW())")
    int insert(AdminInfo info);

    @Update("UPDATE admin_info SET nickname = #{nickname}, avatar = #{avatar}, email = #{email}, gender = #{gender}, " +
            "updated_time = NOW() WHERE admin_id = #{adminId}")
    int updateByAdminId(AdminInfo info);

    /**
     *
     * @param adminId
     * @return
     */
    @Select("SELECT * FROM admin_info WHERE admin_id = #{adminId}")
    AdminInfo getInfoByAdminId(@Param("adminId") String adminId);

    // 查询管理员分页列表
    // 查询管理员分页列表
    @Select("""
    <script>
    SELECT admin.id, admin.phone, admin.is_super AS isSuper,
           admin.created_time, admin.updated_time,
           admin_info.nickname, admin_info.avatar, admin_info.email, admin_info.gender
    FROM admin
    LEFT JOIN admin_info ON admin.id = admin_info.admin_id
    <where>
        <if test="phone != null and phone != ''">
            AND admin.phone LIKE CONCAT('%', #{phone}, '%')
        </if>
        <if test="nickname != null and nickname != ''">
            AND admin_info.nickname LIKE CONCAT('%', #{nickname}, '%')
        </if>
    </where>
    ORDER BY 
        CASE WHEN admin.is_super = '1' THEN 1 ELSE 0 END DESC,
        admin.created_time DESC
    LIMIT #{pageSize} OFFSET #{offset}
    </script>
""")
    List<AdminVO> selectAdmins(@Param("phone") String phone,
                               @Param("nickname") String nickname,
                               @Param("offset") int offset,
                               @Param("pageSize") int pageSize);



    // 查询管理员总数
    @Select("""
        <script>
        SELECT COUNT(1)
        FROM admin
        LEFT JOIN admin_info ON admin.id = admin_info.admin_id
        <where>
            <if test="phone != null and phone != ''">
                AND admin.phone LIKE CONCAT('%', #{phone}, '%')
            </if>
            <if test="nickname != null and nickname != ''">
                AND admin_info.nickname LIKE CONCAT('%', #{nickname}, '%')
            </if>
        </where>
        </script>
    """)
    long countAdmins(@Param("phone") String phone,
                     @Param("nickname") String nickname);

    @Delete("DELETE FROM admin_info WHERE admin_id = #{adminId}")
    void deleteByAdminId(@Param("adminId") String adminId);

    @Select("SELECT id, admin_id AS adminId, nickname, avatar, email, gender, created_time AS createdTime, updated_time AS updatedTime " +
            "FROM admin_info WHERE nickname = #{nickname}")
    AdminInfo selectByNickname(@Param("nickname") String nickname);


}
